import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 静态加载，初始化页面和框架
import Login from '@/views/login'
import Layout from '@/layout/Layout.vue'

// 解决路由1重复点击的bug
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

// 配置路由地址
const routes = []

// 静态路由
export const constantRoutes = [
  // 一级路由
  {
    path: '/',
    redirect: '/home', //登录重定向
    hidden: true,
  },
  {
    path: '/login',
    component: Login,
    hidden: true,
  },
  /**
   * 后台首页
   */
  {
    path: '/home',
    component: Layout, //框架
    meta: {path: '/home', text: '后台首页', icon: 'home'},
    children: [
      {
        path: '',
        component: () => import('@/views/home/index.vue'),
      },
    ],
  },
]

// 动态路由
export const asnycRoutes = [
  /**
   * 订单管理
   */
  {
    path: '/order',
    component: Layout,
    meta: {path: '/order', text: '订单管理', roles: ['super'], icon: 'order'},
    children: [
      {
        path: '',
        component: () => import('@/views/order/index.vue'),
      },
      {
        hidden: true,
        path: '/order/order-detail',
        component: () => import('@/views/order/order-detail'),
      },
      {
        hidden: true,
        path: '/order/order-edit/:id',
        component: () => import('@/views/order/order-edit'),
      },
    ],
  },
  /**
   * 商品管理
   */
  {
    path: '/goods',
    component: Layout,
    redirect: '/goods/goods-list',
    meta: {path: '/goods', text: '商品管理', icon: 'goods'},
    children: [
      {
        path: '/goods/goods-list',
        meta: {path: '/goods/goods-list', text: '商品列表'},
        component: () => import('@/views/goods/goods-list.vue'),
      },
      {
        path: '/goods/goods-add',
        meta: {path: '/goods/goods-add', text: '商品添加', roles: ['super']},
        component: () => import('@/views/goods/goods-add.vue'),
      },
      {
        path: '/goods/goods-cate',
        meta: {path: '/goods/goods-cate', text: '商品分类'},
        component: () => import('@/views/goods/goods-cate.vue'),
      },
    ],
  },
  /**
   * 店铺管理
   */
  {
    path: '/shop',
    component: Layout,
    meta: {path: '/shop', text: '店铺管理', roles: ['super'], icon: 'shop'},
    children: [
      {
        path: '',
        component: () => import('@/views/shop/index.vue'),
      },
    ],
  },
  /**
   * 账号管理
   */
  {
    path: '/account',
    component: Layout,
    redirect: '/account/account-list',
    meta: {path: '/account', text: '账号管理', icon: 'user'},
    children: [
      {
        path: '/account/account-list',
        meta: {
          path: '/account/account-list',
          text: '账号列表',
          roles: ['super'],
        },
        component: () => import('@/views/account/account-list.vue'),
      },
      {
        path: '/account/account-add',
        meta: {
          path: '/account/account-add',
          text: '账号添加',
          roles: ['super'],
        },
        component: () => import('@/views/account/account-add.vue'),
      },
      {
        path: '/account/password',
        meta: {path: '/account/password', text: '密码修改'},
        component: () => import('@/views/account/password-modify.vue'),
      },
      {
        path: '/account/account-person',
        meta: {path: '/account/account-person', text: '个人中心'},
        component: () => import('@/views/account/account-person.vue'),
      },
    ],
  },
  /**
   * 销售管理
   */
  {
    path: '/total',
    component: Layout,
    redirect: '/total/total-goods',
    meta: {path: '/total', text: '销售统计', roles: ['super'], icon: 'total'},
    children: [
      {
        path: '/total/total-goods',
        meta: {path: '/total/total-goods', text: '商品统计'},

        component: () => import('@/views/total/total-goods.vue'),
      },
      {
        path: '/total/total-order',
        meta: {path: '/total/total-order', text: '订单统计'},
        component: () => import('@/views/total/total-order.vue'),
      },
    ],
  },
]

// 错误路由 404 相关
export const errorRoutes = [
  // 404错误
  {
    path: '*',
    redirect: '/404',
  },
  {
    path: '/404',
    component: () => import('@/views/404/error404.vue'),
  },
]

const router = new VueRouter({
  routes: constantRoutes,
})

export default router
